package grid.celebritysocial.logic;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class HttpXml {

	public Document getXmldoc(String url_str) throws SAXException, IOException, ParserConfigurationException{

		URL url = new URL(url_str);
		HttpURLConnection http = (HttpURLConnection)url.openConnection();
		http.setRequestMethod("GET");
		http.connect();

		// XML 取得の準備
		DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dbfactory.newDocumentBuilder();

		// InputStream から Documentオブジェクトを取得
		Document doc = builder.parse(http.getInputStream());

		http.disconnect();
		return doc;

	}

	public NodeList getNodeList(Document doc ,String[] nodeList){
		Element root = doc.getDocumentElement();

		if(nodeList.length!=0){
			NodeList lastNodeList = root.getElementsByTagName(nodeList[0]);
			for(int i=1;i < nodeList.length ; i++){
				Node node = lastNodeList.item(0);
				lastNodeList = ((Element)node).getElementsByTagName(nodeList[i]);
			}
			return lastNodeList;
		}else{
			return null;
		}
	}
}